import {Clazz} from "./Clazz";
import * as THREE from "three";
import {landBlockGeometry, landBlockMaterial} from "../core/Resource";
import * as CANNON from 'cannon-es'

class LandBlock extends Clazz {
    constructor(x = 0, y = 0, z = 0) {
        super("box");
        this.position = new THREE.Vector3(x, y - 0.05, z)
        this.plane = new THREE.Mesh(landBlockGeometry, landBlockMaterial);
        this.plane.rotation.x = (-Math.PI / 2);
        this.plane.position.copy(this.position);
        this.position = this.plane.position;
        this.quaternion = this.plane.quaternion;
        scene.add(this.plane)
        this.createOutline();
        this.size = this.getSize();
        this.outline.scale.set(this.size.x, this.size.y - 0.05, this.size.z)
        this.outline.position.copy(this.position);
        this.outline.quaternion.copy(this.quaternion);
        //scene.add(this.outline);
        this.phyMode = this.createPhyMode();
        console.log(this.position);
    }

    createOutline() {
        let geometry = new THREE.BoxGeometry(1, 1, 1);
        let material = new THREE.MeshBasicMaterial({color: 0x00ff00, opacity: 0.5});
        let cube = new THREE.Mesh(geometry, material);
        this.outline = cube;
    }

    createPhyMode() {
        let sphereShape = new CANNON.Box(new CANNON.Vec3(this.size.x / 2, this.size.y / 2, this.size.z / 2));

        let sphereBody = new CANNON.Body({
            shape: sphereShape,
            position: new CANNON.Vec3(this.position.x, this.position.y, this.position.z),
            type: CANNON.Body.STATIC
        })
        sphereBody.quaternion.copy(this.quaternion);


        physicalWorld.addBody(sphereBody);
        console.log("block", sphereBody.position);

        return sphereBody;
    }

    update() {
        /*this.position.copy(this.phyMode.position);
        this.quaternion.copy(this.phyMode.quaternion);*/
    }

    getSize() {
        return new THREE.Vector3(100, 100, 0.1)
    }

}

export {LandBlock}